/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2021-03-25
 * Time: 19:01
 */
#include "PropertyDialog.h"
#include "DiagramBaseItem.h"

PropertyDialog::PropertyDialog(DiagramBaseItem *item, QWidget *parent)
        : QDialog(parent)
          , item_(item)
          , button_box_(new QDialogButtonBox(this))
{
    InitUI();
    InitEvent();
}

void PropertyDialog::InitUI()
{
    auto *main_layout = new QFormLayout(this);

//    enum ItemData{ IDData,ParentIDData,TitleData,IsRootData,PlaceTypeData,PosXData,PosYData,WidthData,HeightData}
    QMetaEnum m = QMetaEnum::fromType<DiagramBaseItem::ItemData>();
    for (int i = 0; i < m.keyCount(); ++i) {

        if(i == 0 || i == 1 || i == 3 || i == 4)
            continue;

        QString data_name = m.key(i);

        line_edit_map_[data_name] = new QLineEdit(this);
        line_edit_map_[data_name]->setText(item_->data(m.value(i)).toString());

        main_layout->addRow(data_name, line_edit_map_.value(data_name));
    }

    /*[3]: 添加按钮 */
    button_box_->addButton("确定", QDialogButtonBox::ButtonRole::AcceptRole);
    button_box_->addButton("取消", QDialogButtonBox::ButtonRole::RejectRole);

    main_layout->addWidget(button_box_);
}

void PropertyDialog::InitEvent()
{
    connect(button_box_, &QDialogButtonBox::rejected, this, &QDialog::reject);

    connect(button_box_, &QDialogButtonBox::accepted, this, &QDialog::accept);

    /*[1]: 点击确定后，将item属性做对应更改 */
    connect(this,&PropertyDialog::accepted,[=](){

        QMetaEnum m = QMetaEnum::fromType<DiagramBaseItem::ItemData>();
        for (int i = 0; i < m.keyCount(); ++i) {

            if(i == 0 || i == 1 || i == 3 || i == 4)
                continue;

            item_->setData(m.value(i),line_edit_map_.value(m.key(i))->text().trimmed());
        }
    });
}
